package com.useeinfo.oa.modules.task.dao;

import com.useeinfo.framework.extend.dao.CrudDao;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.QueryUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.task.entity.TaskOrderPartner;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther: bin.liu
 * @Date: 2019/7/4 10:25
 * @Description: 任务单参与人
 */
@Repository("taskOrderPartnerDao")
public class TaskOrderPartnerDao implements CrudDao<TaskOrderPartner> {

    private final static Logger logger = LoggerFactory.getLogger(TaskOrderPartnerDao.class);

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Map<String, Object> getSearchCondition(Map<String, String> queryHash) {
        Map<String, Object> conditionHash = new HashMap<>();
        conditionHash.put("s.deleteStatus = ?{paramIndex} ", false);

        if (queryHash == null || queryHash.size() == 0) {
            return conditionHash;
        }

        String id = queryHash.get("id");
        if(StringUtils.isNotBlank(id)){
            conditionHash.put("s.id = ?{paramIndex}", StringConverters.null2Long(id));
        }

        String enterpriseId = queryHash.get("enterpriseId");
        if (StringUtils.isNotBlank(enterpriseId)) {
            conditionHash.put("s.enterprise.id = ?{paramIndex}", StringConverters.null2Long(enterpriseId));
        }
        String partnerId = queryHash.get("partnerId");
        if (StringUtils.isNotBlank(partnerId)) {
            conditionHash.put("s.partner.id = ?{paramIndex}", StringConverters.null2Long(partnerId));
        }
        String taskOrderId = queryHash.get("taskOrderId");
        if (StringUtils.isNotBlank(taskOrderId)) {
            conditionHash.put("s.taskOrder.id = ?{paramIndex}", StringConverters.null2Long(taskOrderId));
        }

        return conditionHash;
    }

    @Override
    public Long totalRecord(Map<String, String> queryHash) {
        Map<String, Object> conditions = getSearchCondition(queryHash);
        TypedQuery<Long> typedQuery = QueryUtils.getTypedQueryByCondition("select count(s) from TaskOrderPartner s ", conditions, entityManager, Long.class);
        return typedQuery.getSingleResult();
    }

    @Override
    public List<TaskOrderPartner> findList(QueryParam queryParam) {
        String sqlInfo = queryParam.joinJPQL("select s from TaskOrderPartner s ", "order by s.id desc ");
        Map<String, Object> conditions = getSearchCondition(queryParam.getSqlMap());
        TypedQuery<TaskOrderPartner> typedQuery = QueryUtils.getTypedQueryByCondition(sqlInfo, conditions, entityManager, TaskOrderPartner.class);
        return queryParam.findPageList(typedQuery);
    }

    @Override
    public TaskOrderPartner findModel(Long id) {
        return entityManager.find(TaskOrderPartner.class, id);
    }

    @Override
    public Integer add(TaskOrderPartner model) {
        model.setCreateDate(new Date());
        entityManager.persist(model);
        logger.info("TaskOrderPartnerDaoImpl添加taskOrderPartner成功！");
        return 1;
    }

    @Override
    public Integer update(TaskOrderPartner model) {
        model.setUpdateDate(new Date());
        entityManager.merge(model);
        return 1;
    }

    @Override
    public Integer delete(Long id) {
        TaskOrderPartner taskOrderPartner = entityManager.find(TaskOrderPartner.class, id);
        entityManager.remove(taskOrderPartner);
        return 1;
    }

    @Override
    public Integer deleteLogic(TaskOrderPartner model) {
        model.setDeleteStatus(true);
        return 1;
    }
}
